package org.eiahe.hr.salary.controller;

import lombok.RequiredArgsConstructor;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.utils.StringUtils;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.core.validate.QueryGroup;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.eiahe.hr.salary.domain.bo.SalaryAdjustmentApplicationBo;
import org.eiahe.hr.salary.domain.vo.SalaryAdjustmentApplicationVo;
import org.eiahe.hr.salary.service.ISalaryAdjustmentApplicationService;
import org.ehe.common.excel.utils.ExcelUtil;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.dev33.satoken.annotation.SaCheckPermission;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 调薪申请Controller
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/salary/adjustment/application")
public class SalaryAdjustmentApplicationController extends BaseController {

    private final ISalaryAdjustmentApplicationService iSalaryAdjustmentApplicationService;

    /**
     * 查询调薪申请列表
     */
    @SaCheckPermission("salaryAdjustment:application:list")
    @GetMapping("/list")
    public TableDataInfo<SalaryAdjustmentApplicationVo> list(SalaryAdjustmentApplicationBo bo, PageQuery pageQuery) {
        return iSalaryAdjustmentApplicationService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出调薪申请列表
     */
    @SaCheckPermission("salaryAdjustment:application:export")
    @Log(title = "调薪申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SalaryAdjustmentApplicationBo bo, HttpServletResponse response) {
        List<SalaryAdjustmentApplicationVo> list = iSalaryAdjustmentApplicationService.queryList(bo);
        ExcelUtil.exportExcel(list, "调薪申请数据", SalaryAdjustmentApplicationVo.class, response);
    }

    /**
     * 获取调薪申请详细信息
     *
     * @param applicationId 主键
     */
    @SaCheckPermission("salaryAdjustment:application:query")
    @GetMapping("/{applicationId}")
    public R<SalaryAdjustmentApplicationVo> getInfo(
            @NotNull(message = "主键不能为空") @PathVariable("applicationId") Long applicationId) {
        return R.ok(iSalaryAdjustmentApplicationService.queryById(applicationId));
    }

    /**
     * 新增调薪申请
     */
    @SaCheckPermission("salaryAdjustment:application:add")
    @Log(title = "调薪申请", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SalaryAdjustmentApplicationBo bo) {
        return toAjax(iSalaryAdjustmentApplicationService.insertByBo(bo));
    }

    /**
     * 修改调薪申请
     */
    @SaCheckPermission("salaryAdjustment:application:edit")
    @Log(title = "调薪申请", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SalaryAdjustmentApplicationBo bo) {
        return toAjax(iSalaryAdjustmentApplicationService.updateByBo(bo));
    }

    /**
     * 删除调薪申请
     *
     * @param applicationIds 主键串
     */
    @SaCheckPermission("salaryAdjustment:application:remove")
    @Log(title = "调薪申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{applicationIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] applicationIds) {
        return toAjax(iSalaryAdjustmentApplicationService.deleteWithValidByIds(Arrays.asList(applicationIds)));
    }

    /**
     * 审批调薪申请(通过)
     */
    @SaCheckPermission("salaryAdjustment:application:approve")
    @Log(title = "调薪申请", businessType = BusinessType.UPDATE)
    @PostMapping("/approve/{applicationId}")
    public R<Void> approve(@NotNull(message = "申请ID不能为空") @PathVariable Long applicationId,
            @RequestParam("approver") String approver,
            @RequestParam(value = "approvalOpinion", required = false) String approvalOpinion) {
        return toAjax(iSalaryAdjustmentApplicationService.approveApplication(applicationId, approver, approvalOpinion));
    }

    /**
     * 拒绝调薪申请
     */
    @SaCheckPermission("salaryAdjustment:application:reject")
    @Log(title = "调薪申请", businessType = BusinessType.UPDATE)
    @PostMapping("/reject/{applicationId}")
    public R<Void> reject(@NotNull(message = "申请ID不能为空") @PathVariable Long applicationId,
            @RequestParam("approver") String approver,
            @RequestParam(value = "approvalOpinion", required = false) String approvalOpinion) {
        return toAjax(iSalaryAdjustmentApplicationService.rejectApplication(applicationId, approver, approvalOpinion));
    }
}
